package com.xiguthings.xiniu.iot.device.state.handl;

import javax.annotation.PostConstruct;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.xiguthings.xiniu.iot.common.DeviceData;
import com.xiguthings.xiniu.iot.common.entity.MessageProducerRule;
import com.xiguthings.xiniu.iot.device.state.controller.DataCenter;
import com.xiguthings.xiniu.iot.device.state.jedis.JedisClient;
import com.xiguthings.xiniu.iot.device.state.kafka.KafkaClient;
import com.xiguthings.xiniu.iot.etce.ProjectMessageEtcdClient;

@Component
public class EventHandler {
	@Autowired
	private JedisClient jedisClient;
	@Autowired
	@Lazy
	private DataCenter dataCenter;

	@Autowired
	private KafkaClient kafkaClient;

	public EventHandler() {
		super();
	}

	@PostConstruct
	private void init() {
		// dataCenter = SpringUtils.getBean(DataCenter.class);
		// jedisClient = SpringUtils.getBean(JedisClient.class);
	}

	/**
	 * 处理proxy变更事件
	 * 
	 * @param deviceData
	 */
	public void handleProxyChange(DeviceData deviceData) {
		String queryOldProxyId = dataCenter.queryOldProxyId(deviceData);
		if (StringUtils.isBlank(queryOldProxyId)) {
			return;
		}
		// 判断之前的proxy和现在的proxy是否一致
		if (queryOldProxyId.equals(deviceData.getHead().getProxyId())) {
			return;
		}
		Thread thread = new Thread(new Runnable() {

			@Override
			public void run() {
				// 删除数据库中保存的proxy下的设备
				jedisClient.hdel(deviceData.getHead().getProxyId(), deviceData.getHead().getDeviceId().toString());
				// 将变更告警发送到平台
				MessageProducerRule messageProducerRule = ProjectMessageEtcdClient
						.messageProducerRuleByProjectId(deviceData.getHead().getProjectId());
				if (messageProducerRule == null || messageProducerRule.getTopic() == null
						|| messageProducerRule.getPartition() == null) {
					return;
				}
				JSONObject messageJson = makeMessage(deviceData);
				if (messageJson == null) {
					return;
				}
				String message = messageJson.toJSONString();
				String toipc = messageProducerRule.getTopic();
				Integer partition = new Integer(messageProducerRule.getPartition());
				kafkaClient.produceData(message, toipc, partition);
			}
		});
		thread.start();
	}

	/**
	 * 生成消息
	 * 
	 * @param deviceData
	 * @param rule
	 * @param resStr
	 * @return
	 */
	private JSONObject makeMessage(DeviceData deviceData) {
		JSONObject message = new JSONObject();
		message.put("rule", "system-buildin");
		message.put("name", "DeviceProxyChanged");
		message.put("time", ((long) (System.currentTimeMillis() * 0.001)));
		message.put("serverity", "info");
		message.put("producer", "iot-storage-redis");
		JSONObject data = new JSONObject();
		data.put("tags", deviceData.getHead());
		data.put("data", deviceData.getBody());
		message.put("body", data);
		return message;
	}
}
